//
//  StatusTableViewCell.swift
//  我的微博
//
//  Created by 杨庆 on 15/10/25.
//  Copyright © 2015年 双元班. All rights reserved.
//

import UIKit
//魔法数字的定义,方便代码的评审!
let statusCellControlMargin:CGFloat = 8.0
class StatusTableViewCell: UITableViewCell {
    ///微博数据模型
    var statuses: status? {
        didSet{
        topview.statuses = statuses
        contentLabel.text = statuses?.text ?? ""
        //测试的修改 picture 的高度
        //注意的是直接的将 模型丢给属性
        //设置配图视图的数据(封装的高度的演练)
        pictureView.statuses = statuses
        //TODO: 行高的高度(方法有改变)
//        pictureHightCons?.constant = CGFloat(arc4random() % 3) * 90
        //设置配图视图的尺寸
        pictureHightCons?.constant = pictureView.bounds.size.height
        pictureWidthCons?.constant = pictureView.bounds.size.width
            
        
        }
        
        
    }
    //记录的可变的宽高的约束
    var pictureWidthCons: NSLayoutConstraint?
    var pictureHightCons: NSLayoutConstraint?
    
    //MARK:-界面的设置;
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 设置界面
    private func setupUI(){
        //1.添加控件
        contentView.addSubview(topview)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        //2.设置布局
        //1>顶部视图
        topview.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        //2>内容标签
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topview, size: nil, offset: CGPoint(x: statusCellControlMargin, y: statusCellControlMargin))
        //设置内容的宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * statusCellControlMargin))
        //3>配图视图
        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView:contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: statusCellControlMargin))
        pictureHightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        pictureWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
        //4>底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -statusCellControlMargin, y: statusCellControlMargin))
        //底部约束
        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
    }
    //MARK:-懒加载控件;
    /// 添加顶部视图
    private lazy var topview: topView = topView()
    /// 添加内容标签
    private lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.lightGrayColor(), fontSize: 15)
        label.numberOfLines = 0
        return label
    }()
    /// 添加图片配图视图
    private lazy var pictureView: statusPictureView = statusPictureView()
    /// 添加底部视图
    private lazy var bottomView: statusBottomView = statusBottomView()
}
